﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Server.Common.Entity;
using System.Configuration;
using System.Data;
using BySoft.Server.Business;
using BySoft.Server.Consts;
using BySoft.Server.Common.Schema;

namespace BySoft.Web.Site.UserControl
{
    public partial class TopMenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //动态菜单
            if (Session["Login_User"] != null)
            {
                SysUserEntity objUser = ((SysUserEntity)Session["Login_User"]);

                //////////
                #region 菜单

                bool IsRightEnabled = ConfigurationManager.AppSettings["Right_Enable"].ToString().Equals("1") ? true : false;//权限是否开启

                //根据当前用户，获得他所拥有的权限，来控制菜单
                string userId = "";
                if (IsRightEnabled)//权限是否开启
                {
                    userId = objUser.UserID;
                }

                DataTable dtTopMenulist = new DataTable();
                DataTable dtLeftMenuList = new DataTable();

                //根据USERID获得此用户的角色信息
                //DataTable dtUserRole = SysUserrole.GetRoleByUser(objUser.Userid);
                DataTable dtUserRole = SysUserrole.GetRoleByUserAndUserType(objUser.UserID);


                //如果此用户的所属角色ID中有ADMIN_ROLE_ID=1的
                if (!IsRightEnabled || dtUserRole.Select(SysRoleTable.C_ROLEID + "='" + CommonConsts.Admin_Role_ID + "'").Length > 0 || objUser.UserID == CommonConsts.ADMIN_USER || objUser.UserID == "ByAdmin")//系统管理员不检查权限
                {
                    dtTopMenulist = SysModule.MyGetTopModuleListByAdmin();  //s
                    dtLeftMenuList = SysModule.MyGetSedMenuListByAdmin();  //f
                }
                else
                {
                    dtTopMenulist = SysModule.GetTopModuleListByUser(userId);  //s
                    dtLeftMenuList = SysModule.GetSedMenuListByUser(userId);  //f
                }

                if (dtTopMenulist == null ||
                    dtTopMenulist.Rows.Count < 1 ||
                    dtLeftMenuList == null ||
                    dtLeftMenuList.Rows.Count < 1)
                {
                    Session["Login_User"] = null;
                    Session["Login_FactoryType"] = null;
                }

                string strHtml = "<ul id=\"nav\" class=\"warp\">";
                Session["TopModulelist"] = dtTopMenulist;
                Session["LeftModulelist"] = dtLeftMenuList;

                for (int i = 0; i < dtTopMenulist.Rows.Count; i++)
                {
                    DataRow[] drs = dtLeftMenuList.Select(SysModuleTable.C_PARENTMODULEID + "='" + dtTopMenulist.Rows[i][SysModuleTable.C_MODULEID].ToString() + "'", SysModuleTable.C_ORDERNO);

                    if (dtTopMenulist.Rows[i][SysModuleTable.C_MODULEID].ToString().Equals("FS01"))
                    {
                        string strUrl = "Main.aspx";
                        string strmenu1 = "menu" + strUrl.Replace("/", "").Replace(".aspx", "").Replace("?", "").Replace("=", "");
                        strHtml += "<li><a style=\"\" href=\"../" + strUrl + "\"  id=\"" + strUrl.Replace("/", "").Replace(".aspx", "") + "\" target=\"content\" onclick=\"easytabs('1', 'FS01');seftleftmenu('" + strUrl + "')\"><span id='" + strmenu1 + "' >";
                        strHtml += dtTopMenulist.Rows[i][SysModuleTable.C_MODULENAME].ToString() + "</span></a></li>";
                    }
                    else
                    {
                        strHtml += "<li><a href=\"#\" onclick=\"easytabs('1', '" + dtTopMenulist.Rows[i][SysModuleTable.C_MODULEID].ToString() + "');\"  id=\"tablink" + dtTopMenulist.Rows[i][SysModuleTable.C_MODULEID].ToString() + "\" ><span id='menu" + dtTopMenulist.Rows[i][SysModuleTable.C_MODULEID].ToString() + "'>" + dtTopMenulist.Rows[i][SysModuleTable.C_MODULENAME].ToString() + "</span></a></li>";
                    }

                }
                strHtml += "</ul>";

                strHtml += "<div class=\"navi\">";
                for (int i = 0; i < dtTopMenulist.Rows.Count; i++)
                {
                    string sClass = "";
                    if (i == 0)
                    {
                        sClass = "naviWarp current";
                    }
                    else
                    {
                        sClass = "naviWarp hide";
                    }
                    strHtml += "<div id=\"tabcontent" + dtTopMenulist.Rows[i][SysModuleTable.C_MODULEID].ToString() + "\" class=\"" + sClass + "\">";
                    DataRow[] drs = dtLeftMenuList.Select(SysModuleTable.C_PARENTMODULEID + "='" + dtTopMenulist.Rows[i][SysModuleTable.C_MODULEID].ToString() + "'", SysModuleTable.C_ORDERNO);
                    for (int j = 0; j < drs.Length; j++)
                    {
                        string sUrl = drs[j][SysModuleTable.C_FUNCTIONURL].ToString();// +sJump;

                        string strmenu = "menu" + sUrl.Replace("/", "").Replace(".aspx", "").Replace("?", "").Replace("=", "");

                        strHtml += "<a style=\"\" href=\"../" + sUrl + "\"  id=\"" + sUrl.Replace("/", "").Replace(".aspx", "") + "\" target=\"content\" onclick=\"seftleftmenu('" + sUrl + "')\"><span id='" + strmenu + "' style=\"color: White\">";
                        strHtml += drs[j][SysModuleTable.C_MODULENAME].ToString() + "</span></a>";

                    }
                    strHtml += "</div>";

                }
                strHtml += "</div>";
                divTopMenu.InnerHtml = strHtml;
                #endregion

            }
        }
    }
}